package com.bicon.botu.file.upload.common;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lenovo on 2016/9/20.
 */
public class ImageUtil {


    /**
     * 缩放图像（按比例缩放）
     *
     * @param from   源图像文件地址(绝对路径)
     * @param target 缩放后的图像地址(绝对路径)
     * @param scale  缩放比例
     */
    public final static void scale(File from, File target,
                                   int scale) {
        try {
            BufferedImage src = ImageIO.read(from); // 读入文件
            int width = src.getWidth(); // 得到源图宽
            int height = src.getHeight(); // 得到源图
            width = width / scale;
            height = height / scale;

            Image image = src.getScaledInstance(width, height,
                    Image.SCALE_DEFAULT);
            BufferedImage tag = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            g.drawImage(image, 0, 0, null); // 绘制缩小后的图
            g.dispose();
            ImageIO.write(tag, "JPEG", target);// 输出到文件流
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 缩放图像（按高度和宽度缩放）
     *
     * @param srcImageFile 源图像文件地址(绝对路径)
     * @param result       缩放后的图像地址(绝对路径)
     * @param height       缩放后的高度
     * @param width        缩放后的宽度
     */
    public final static Map<String, Integer> scale(String srcImageFile, String result,
                                                   int width, int height) {
        Map<String, Integer> data = new HashMap<String, Integer>();
        try {
            double ratio = 0.0; // 缩放比例
            Image srcFile = ImageIO.read(new File(srcImageFile));
            int w = srcFile.getWidth(null);
            int h = srcFile.getHeight(null);
            if (width > 0) {
                ratio = width / (double) w;
                height = (int) (h * ratio);
            } else {
                if (height > 0) {
                    ratio = height / (double) h;
                    width = (int) (w * ratio);
                }
            }
            if (0 == width && 0 == height) {
                ratio = 0.5;
                width = (int) (w * ratio);
                height = (int) (h * ratio);
            }
            File f = new File(srcImageFile);
            BufferedImage bi = ImageIO.read(f);
            // 计算比例
            /*if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
                if (bi.getHeight() > bi.getWidth()) {
                    ratio = (new Integer(height)).doubleValue()
                            / bi.getHeight();
                } else {
                    ratio = (new Integer(width)).doubleValue() / bi.getWidth();
                }*/
            AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
            BufferedImage bii = null;
            bii = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            bii.getGraphics().drawImage(
                    bi.getScaledInstance(w, h, Image.SCALE_SMOOTH), 0, 0, null);
            srcFile = op.filter(bii, null);
            ImageIO.write((BufferedImage) srcFile, "JPEG", new File(result));
            data.put("w", w);
            data.put("h", h);
            data.put("width", width);
            data.put("height", height);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }


}
